package com.crape.account.bo.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.crape.account.bo.IDarrenIndexBO;
import com.crape.account.dao.IAccountRefDAO;
import com.crape.account.dao.IDarrenIndexDAO;
import com.crape.common.bean.LoginUser;
import com.crape.common.enums.AttentionType;
import com.crape.common.exceptions.BOException;
import com.crape.common.model.AccountInfo;
import com.crape.common.model.DarrenIndex;
import com.crape.common.thread.ThreadObjManager;

public class DarrenIndexBOImpl implements IDarrenIndexBO {
	
	private IDarrenIndexDAO darrenIndexDAOImpl;
	private IAccountRefDAO accountRefDAOImpl;
	
	@Autowired
	public void setDarrenIndexDAOImpl(IDarrenIndexDAO darrenIndexDAOImpl) {
		this.darrenIndexDAOImpl = darrenIndexDAOImpl;
	}
	
	@Autowired
	public void setAccountRefDAOImpl(IAccountRefDAO accountRefDAOImpl) {
		this.accountRefDAOImpl = accountRefDAOImpl;
	}

	@Override
	public void recommendDarren(Long accountid, Integer indexType) throws BOException {
		if(accountid==null||indexType==null){
			return;
		}
		DarrenIndex darrenIndex = darrenIndexDAOImpl.getDarrenIndexBydarrenid(accountid);
		DarrenIndex params = new DarrenIndex();
		params.setDarrenid(accountid);
		params.setType(indexType);
		if(darrenIndex==null){
			darrenIndexDAOImpl.insertDarrenIndex(params);
		}else{
			darrenIndexDAOImpl.updateDarrenIndex(params);
		}
	}

	@Override
	public List<AccountInfo> getIndexDarrens(Long accountid, String email, Integer indexType, Integer startIndex, Integer pageSize) throws BOException {
		return darrenIndexDAOImpl.getIndexDarrens(accountid, email, indexType, startIndex, pageSize);
	}

	@Override
	public Long countIndexDarrens(Long accountid, String email, Integer indexType) throws BOException {
		return darrenIndexDAOImpl.countIndexDarrens(accountid, email, indexType);
	}

	@Override
	public List<AccountInfo> getIndexShowDarrens(Integer indexType, Integer showNum) throws BOException {
		List<AccountInfo> list = darrenIndexDAOImpl.getIndexShowDarrens(indexType, showNum);
		if(list==null){
			list = new ArrayList<AccountInfo>();
		}
		LoginUser loginUser = ThreadObjManager.getLoginUser();
        Long accountid = loginUser.getAccountid();
        if (accountid != null) {
			for (AccountInfo accountInfo : list) {
				Long num = accountRefDAOImpl.countAcctRefs(accountid, accountInfo.getAccountid());
				if (num != null && num > 0) {
					accountInfo.setIsattention(AttentionType.yes.getCode());
				} else {
					accountInfo.setIsattention(AttentionType.no.getCode());
				}
			}
		}
		return list;
	}

}
